
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:tencent_rtc/bindings/trtc_cloud_native.dart';
import 'package:tencent_rtc/bridge/trtc_method_channel.dart';
import 'package:tencent_rtc/impl/tx_audio_effect_manager_impl.dart';
import 'package:tencent_rtc/impl/tx_beauty_manager_impl.dart';
import 'package:tencent_rtc/impl/tx_device_manager_impl.dart';
import 'package:tencent_rtc/trtc_cloud_def.dart';
import 'package:tencent_rtc/trtc_cloud_listener.dart';
import 'package:tencent_rtc/trtc_cloud_video_view.dart';
import 'package:tencent_rtc/tx_audio_effect_manager.dart';
import 'package:tencent_rtc/tx_device_manager.dart';

import 'package:tencent_rtc/bindings/trtc_cloud_listener_native.dart';
import 'package:tencent_rtc/trtc_cloud.dart';

class TRTCCloudImpl extends TRTCCloud {
  final _tag = 'TRTCCloudImpl';
  static final _trtc = TRTCCloudImpl();
  static final TRTCLogParams _logParams = TRTCLogParams();
  static bool _isInit = false;

  late TXDeviceManager _deviceManager;
  late TXAudioEffectManager _audioEffectManager;

  TRTCCloudListenerNative? _listenerNative;

  TRTCAudioFrameCallbackNative? _audioFrameCallbackNative;
  TRTCLogCallbackNative? _logCallbackNative;

  static Future<TRTCCloud> sharedInstance() async {
    if (!_isInit) {
      await TRTCMethodChannel().initialize();
      TRTCCloudNative.sharedInstance();
      _isInit = true;
    }
    return _trtc;
  }

  static void destroySharedInstance() {
    TXDeviceManagerImpl.destroyDeviceManager();
    TXBeautyManagerImpl.destroyBeautyManager();
    TXAudioEffectManagerImpl.destroyAudioEffectManager();

    _trtc._logCallbackNative?.unRegisterNativeListener();
    _trtc._logCallbackNative = null;

    _trtc._audioFrameCallbackNative?.unRegisterNativeListener();
    _trtc._audioFrameCallbackNative = null;

    _trtc._listenerNative?.unRegisterNativeListener();
    _trtc._listenerNative = null;

    _isInit = false;
    TRTCCloudNative.destroySharedInstance();
  }
  
  static void TRTCLog(String tag, String body) {
    TRTCCloudNative.instance.writeLog(0, "trtcplugin", tag, body);
  }

  @override
  void registerListener(TRTCCloudListener func) {
    _listenerNative ??= TRTCCloudListenerNative(TRTCCloudNative.sharedInstanceNativePointer);
    _listenerNative?.addListener(func);
  }

  @override
  void unRegisterListener(TRTCCloudListener func) {
    _listenerNative?.removeListener(func);
  }

  @override
  void enterRoom(TRTCParams param, TRTCAppScene scene) {
    TRTCLog(_tag, "enterRoom sdkappid: ${param.sdkAppId} userId: ${param.userId}");
    TRTCCloudNative.instance.callExperimentalAPI("{\"api\": \"setFramework\", \"params\": {\"framework\": 7, \"language\": 9}}");
    TRTCCloudNative.instance.enterRoom(param, scene.value());
  }


  @override
  TXDeviceManager getDeviceManager() {
    TRTCLog(_tag, "getDeviceManager");
    _deviceManager = TXDeviceManagerImpl(TRTCCloudNative.instance.getDeviceManager());
    return _deviceManager;
  }

  @override
  void enableAudioVolumeEvaluation(bool enable, TRTCAudioVolumeEvaluateParams params) {
    TRTCLog(_tag, "enableAudioVolumeEvaluation enable: $enable");
    TRTCCloudNative.instance.enableAudioVolumeEvaluation(enable, params);
  }

  @override
  void exitRoom() {
    TRTCLog(_tag, "exitRoom");
    TRTCCloudNative.instance.exitRoom();
  }

  @override
  void muteLocalVideo(TRTCVideoStreamType streamType, bool mute) {
    TRTCLog(_tag, "muteLocalVideo streamType: $streamType mute: $mute");
    TRTCCloudNative.instance.muteLocalVideo(streamType.value(), mute);
  }

  @override
  void muteLocalAudio(bool mute) {
    TRTCLog(_tag, "muteLocalAudio mute: $mute");
    TRTCCloudNative.instance.muteLocalAudio(mute);
  }

  @override
  void muteRemoteVideoStream(String userId, TRTCVideoStreamType streamType, bool mute) {
    TRTCLog(_tag, "muteRemoteVideoStream userId: $userId streamType: $streamType mute: $mute");
    TRTCCloudNative.instance.muteRemoteVideoStream(userId, streamType.value(), mute);
  }

  @override
  void setGravitySensorAdaptiveMode(TRTCGSensorMode mode) {
    TRTCLog(_tag, "setGravitySensorAdaptiveMode mode: $mode");
    TRTCCloudNative.instance.setGravitySensorAdaptiveMode(mode.value());
  }

  @override
  void setLocalRenderParams(TRTCRenderParams params) {
    TRTCLog(_tag, "setLocalRenderParams rotation: ${params.rotation} fillMode: ${params.fillMode} mirrorType: ${params.mirrorType}");
    TRTCCloudNative.instance.setLocalRenderParams(params.rotation.value(), params.fillMode.value(), params.mirrorType.value());
  }

  @override
  void setNetworkQosParam(TRTCNetworkQosParam params) {
    TRTCLog(_tag, "setNetworkQosParam preference: ${params.preference}");
    TRTCCloudNative.instance.setNetworkQosParam(params);
  }

  @override
  void setVideoEncoderParam(TRTCVideoEncParam params) {
    TRTCLog(_tag, "setVideoEncoderParam");
    TRTCCloudNative.instance.setVideoEncoderParam(params);
  }

  @override
  void startLocalAudio(TRTCAudioQuality quality) {
    TRTCLog(_tag, "startLocalAudio quality: $quality");
    TRTCCloudNative.instance.startLocalAudio(quality.value());
  }

  @override
  void startLocalPreview(bool frontCamera, int viewId) async {
    TRTCLog(_tag, "startLocalPreview frontCamera: $frontCamera viewId: $viewId");
    if (TRTCPlatform.isMacOS || TRTCPlatform.isWindows) {
      await TRTCMethodChannel().startLocalPreview(viewId);
      return;
    }
    if (!TRTCCloudVideoView.containsViewId(viewId)) {
      TRTCLog(_tag, "startLocalPreview fail, viewId does not exist");
      return;
    }
    TRTCCloudNative.instance.startLocalPreview(frontCamera, viewId);
  }

  @override
  void startRemoteView(String userId, TRTCVideoStreamType streamType, int viewId) async {
    TRTCLog(_tag, "startRemoteView userId: $userId streamType: $streamType viewId: $viewId");
    if (TRTCPlatform.isMacOS || TRTCPlatform.isWindows) {
      await TRTCMethodChannel().startRemoteView(userId, streamType, viewId);
      return;
    }
    if (!TRTCCloudVideoView.containsViewId(viewId)) {
      TRTCLog(_tag, "startRemoteView fail, viewId does not exist");
      return;
    }
    TRTCCloudNative.instance.startRemoteView(userId, streamType.value(), viewId);
  }

  @override
  void stopAllRemoteView() {
    TRTCLog(_tag, "stopAllRemoteView");
    TRTCCloudNative.instance.stopAllRemoteView();
  }

  @override
  void stopLocalAudio() {
    TRTCLog(_tag, "stopLocalAudio");
    TRTCCloudNative.instance.stopLocalAudio();
  }

  @override
  void stopLocalPreview() {
    TRTCLog(_tag, "stopLocalPreview");
    if (TRTCPlatform.isMacOS || TRTCPlatform.isWindows) {
      TRTCMethodChannel().stopLocalPreview();
      return;
    }
    TRTCCloudNative.instance.stopLocalPreview();
  }

  @override
  void stopRemoteView(String userId, TRTCVideoStreamType streamType) {
    TRTCLog(_tag, "stopRemoteView userId: $userId streamType: $streamType");
    if (TRTCPlatform.isMacOS || TRTCPlatform.isWindows) {
      TRTCMethodChannel().stopRemoteView(userId, streamType);
      return;
    }
    TRTCCloudNative.instance.stopRemoteView(userId, streamType.value());
  }

  @override
  void switchRole(TRTCRoleType role) {
    TRTCLog(_tag, "switchRole role: $role");
    TRTCCloudNative.instance.switchRole(role.value());
  }

  @override
  void switchRoom(TRTCSwitchRoomConfig config) {
    TRTCLog(_tag, "switchRoom roomId: ${config.roomId} strRoomId: ${config.strRoomId}");
    TRTCCloudNative.instance.switchRoom(config);
  }

  @override
  String callExperimentalAPI(String jsonStr) {
    TRTCLog(_tag, "callExperimentalAPI jsonStr: $jsonStr");
    if (!jsonStr.contains("enableVideoProcessByNative")) {
      return TRTCCloudNative.instance.callExperimentalAPI(jsonStr);
    }

    try {
      TRTCMethodChannel().enableVideoProcessByNative(jsonStr);
      return "success";
    } catch (e) {
      TRTCLog(_tag, "Error in callExperimentalAPI: jsonStr: $jsonStr, error: $e");
      return '';
    }
  }

  @override
  void connectOtherRoom(String param) {
    TRTCLog(_tag, "connectOtherRoom param: $param");
    TRTCCloudNative.instance.connectOtherRoom(param);
  }

  // @override
  // TRTCCloud createSubCloud() {
  //   // TODO: implement createSubCloud
  //   throw UnimplementedError();
  // }
  //
  // @override
  // void destroySubCloud(TRTCCloud subCloud) {
  //   // TODO: implement destroySubCloud
  // }

  @override
  void disconnectOtherRoom() {
    TRTCLog(_tag, "disconnectOtherRoom");
    TRTCCloudNative.instance.disconnectOtherRoom();
  }

  @override
  void enableCustomAudioCapture(bool enable) {
    TRTCLog(_tag, "enableCustomAudioCapture: $enable");
    TRTCCloudNative.instance.enableCustomAudioCapture(enable);
  }

  @override
  // void enableLocalVideoCustomProcess(bool enable, TRTCVideoPixelFormat format, TRTCVideoBufferType type) {
  //   TRTCLog(_tag, "enableLocalVideoCustomProcess: $enable format: $format type: $type");
  //   TRTCCloudNative.instance.enableLocalVideoCustomProcess(enable, format.value(), type.value());
  // }

  @override
  void enableMixExternalAudioFrame(bool enablePublish, bool enablePlayout) {
    TRTCLog(_tag, "enableMixExternalAudioFrame: $enablePublish enablePlayout: $enablePlayout");
    TRTCCloudNative.instance.enableMixExternalAudioFrame(enablePublish, enablePlayout);
  }

  @override
  int enableSmallVideoStream(bool enable, TRTCVideoEncParam smallVideoEncParam) {
    TRTCLog(_tag, "enableSmallVideoStream: $enable smallVideoEncParam: $smallVideoEncParam");
    return TRTCCloudNative.instance.enableSmallVideoStream(enable, smallVideoEncParam);
  }

  @override
  int getAudioCaptureVolume() {
    TRTCLog(_tag, "getAudioCaptureVolume");
    return TRTCCloudNative.instance.getAudioCaptureVolume();
  }

  @override
  TXAudioEffectManager getAudioEffectManager() {
    TRTCLog(_tag, "getAudioEffectManager");
    _audioEffectManager = TXAudioEffectManagerImpl(TRTCCloudNative.instance.getAudioEffectManager());
    return _audioEffectManager;
  }

  @override
  int getAudioPlayoutVolume() {
    TRTCLog(_tag, "getAudioPlayoutVolume");
    return TRTCCloudNative.instance.getAudioPlayoutVolume();
  }

  @override
  String getSDKVersion() {
    TRTCLog(_tag, "getSDKVersion");
    return TRTCCloudNative.instance.getSDKVersion();
  }

  @override
  TRTCScreenCaptureSourceList? getScreenCaptureSources(TRTCSize thumbnail, TRTCSize icon) {
    TRTCLog(_tag, "getScreenCaptureSources: thumbnail: $thumbnail icon: $icon");
    if (Platform.isAndroid || Platform.isIOS) {
      debugPrint("trtc-api not support");
      return null;
    }
    return TRTCCloudNative.instance.getScreenCaptureSources(thumbnail, icon);
  }

  @override
  void muteAllRemoteAudio(bool mute) {
    TRTCLog(_tag, "muteAllRemoteAudio: $mute");
    TRTCCloudNative.instance.muteAllRemoteAudio(mute);
  }

  @override
  void muteRemoteAudio(String userId, bool mute) {
    TRTCLog(_tag, "muteRemoteAudio: $userId mute: $mute");
    TRTCCloudNative.instance.muteRemoteAudio(userId, mute);
  }

  @override
  void pauseScreenCapture() {
    TRTCLog(_tag, "pauseScreenCapture");
    TRTCCloudNative.instance.pauseScreenCapture();
  }

  @override
  void resumeScreenCapture() {
    TRTCLog(_tag, "resumeScreenCapture");
    TRTCCloudNative.instance.resumeScreenCapture();
  }

  @override
  void selectScreenCaptureTarget(TRTCScreenCaptureSourceInfo source, TRTCRect rect, TRTCScreenCaptureProperty property) {
    TRTCLog(_tag, "selectScreenCaptureTarget: source: $source rect: $rect property: $property");
    if (Platform.isAndroid || Platform.isIOS) {
      debugPrint("trtc-api not support");
      return;
    }
    TRTCCloudNative.instance.selectScreenCaptureTarget(source, rect, property);
  }

  @override
  void sendCustomAudioData(TRTCAudioFrame frame) {
    TRTCLog(_tag, "sendCustomAudioData: frame: $frame");
    TRTCCloudNative.instance.sendCustomAudioData(frame);
  }

  @override
  bool sendCustomCmdMsg(int cmdID, String data, bool reliable, bool ordered) {
    TRTCLog(_tag, "sendCustomCmdMsg: cmdID: $cmdID data: $data reliable: $reliable ordered: $ordered");
    return TRTCCloudNative.instance.sendCustomCmdMsg(cmdID, data, reliable, ordered);
  }

  @override
  void sendCustomVideoData(TRTCVideoStreamType streamType, TRTCVideoFrame frame) {
    TRTCLog(_tag, "sendCustomVideoData: streamType: $streamType frame: $frame");
    TRTCCloudNative.instance.sendCustomVideoData(streamType, frame);
  }

  @override
  bool sendSEIMsg(String data, int repeatCount) {
    TRTCLog(_tag, "sendSEIMsg: data: $data repeatCount: $repeatCount");
    return TRTCCloudNative.instance.sendSEIMsg(data, repeatCount);
  }

  @override
  void setAudioCaptureVolume(int volume) {
    TRTCLog(_tag, "setAudioCaptureVolume: $volume");
    TRTCCloudNative.instance.setAudioCaptureVolume(volume);
  }

  // @override
  // int setAudioFrameCallback(TRTCAudioFrameCallback? callback) {
  //   TRTCLog(_tag, "setAudioFrameCallback: $callback");
  //   if (callback != null) {
  //     _audioFrameCallbackNative ??= TRTCAudioFrameCallbackNative(TRTCCloudNative.sharedInstanceNativePointer);
  //     _audioFrameCallbackNative?.addListener(callback);
  //   } else {
  //     if (_audioFrameCallbackNative != null) {
  //       _audioFrameCallbackNative?.clearListeners();
  //     }
  //   }
  //   return 0;
  // }

  @override
  void setAudioPlayoutVolume(int volume) {
    TRTCLog(_tag, "setAudioPlayoutVolume: $volume");
    TRTCCloudNative.instance.setAudioPlayoutVolume(volume);
  }

  @override
  void setDefaultStreamRecvMode(bool autoRecvAudio, bool autoRecvVideo) {
    TRTCLog(_tag, "setDefaultStreamRecvMode: autoRecvAudio: $autoRecvAudio autoRecvVideo: $autoRecvVideo");
    TRTCCloudNative.instance.setDefaultStreamRecvMode(autoRecvAudio, autoRecvVideo);
  }

  @override
  // void setLocalVideoCustomProcessCallback(TRTCVideoFrameCallback? callback) {
  //   // TODO: implement setLocalVideoCustomProcessCallback
  // }

  @override
  // int setLocalVideoRenderCallback(TRTCVideoPixelFormat format, TRTCVideoBufferType type, TRTCVideoRenderCallback? callback) {
  //   // TODO: implement setLocalVideoRenderCallback
  //   throw UnimplementedError();
  // }

  @override
  void setLogCallback(TRTCLogCallback? callback) {
    TRTCLog(_tag, "setLogCallback: $callback");
    if (callback != null) {
      _logCallbackNative ??= TRTCLogCallbackNative(TRTCCloudNative.sharedInstanceNativePointer);
      _logCallbackNative?.addListener(callback);
    } else {
      if (_logCallbackNative != null) {
        _logCallbackNative?.clearListeners();
      }
    }
  }

  // @override
  // void setLogParams(TRTCLogParams params) {
  //   TRTCLog(_tag, "setLogParams: $params");
  //   TRTCCloudNative.instance.setLogParams(params);
  // }

  @override
  void setRemoteAudioVolume(String userId, int volume) {
    TRTCLog(_tag, "setRemoteAudioVolume: $userId $volume");
    TRTCCloudNative.instance.setRemoteAudioVolume(userId, volume);
  }

  @override
  void setRemoteRenderParams(String userId, TRTCVideoStreamType streamType, TRTCRenderParams params) {
    TRTCLog(_tag, "setRemoteRenderParams: $userId $streamType $params");
    TRTCCloudNative.instance.setRemoteRenderParams(userId, streamType, params);
  }

  @override
  // int setRemoteVideoRenderCallback(String userId, TRTCVideoPixelFormat format, TRTCVideoBufferType type, TRTCVideoRenderCallback? callback) {
  //   // TODO: implement setRemoteVideoRenderCallback
  //   throw UnimplementedError();
  // }

  @override
  void setRemoteVideoStreamType(String userId, TRTCVideoStreamType streamType) {
    TRTCLog(_tag, "setRemoteVideoStreamType: $userId $streamType");
    TRTCCloudNative.instance.setRemoteVideoStreamType(userId, streamType);
  }

  @override
  void setSubStreamEncoderParam(TRTCVideoEncParam param) {
    TRTCLog(_tag, "setSubStreamEncoderParam: $param");
    TRTCCloudNative.instance.setSubStreamEncoderParam(param);
  }

  @override
  void setWatermark(String imagePath, TRTCVideoStreamType streamType, double x, double y, double width) {
    if (Platform.isWindows || Platform.isMacOS) {
      TRTCLog(_tag, "setWatermark is not currently supported on this platform");
      return;
    }
    TRTCLog(_tag, "setWatermark: $imagePath $streamType $x $y $width");
    TRTCMethodChannel().setWatermark(imagePath, streamType, x, y, width);
  }

  @override
  int startLocalRecording(TRTCLocalRecordingParams param) {
    TRTCLog(_tag, "startLocalRecording: $param");
    return TRTCCloudNative.instance.startLocalRecording(param);
  }

  @override
  void startPublishMediaStream(TRTCPublishTarget target, TRTCStreamEncoderParam param, TRTCStreamMixingConfig config) {
    TRTCLog(_tag, "startPublishMediaStream: $target $param $config");
    TRTCCloudNative.instance.startPublishMediaStream(target, param, config);
  }

  @override
  void startScreenCapture(int viewId, TRTCVideoStreamType streamType, TRTCVideoEncParam encParam) {
    TRTCLog(_tag, "startScreenCapture: viewId:$viewId, streamType:$streamType, encParam:$encParam");
    if (TRTCPlatform.isIOS) {
      TRTCMethodChannel().startScreenCapture(viewId, streamType, encParam);
      return;
    }
    if ((TRTCPlatform.isMacOS || TRTCPlatform.isWindows) && viewId != 0) {
      viewId = 0;
      TRTCLog(_tag, "The current platform does not support local preview");
    }
    if (viewId != 0 && !TRTCCloudVideoView.containsViewId(viewId)) {
      TRTCLog(_tag, "startScreenCapture fail, viewId does not exist");
      return;
    }
    TRTCCloudNative.instance.startScreenCapture(viewId, streamType, encParam);
  }

  @override
  void startScreenCaptureByReplaykit(TRTCVideoStreamType streamType, TRTCVideoEncParam encParam, String? appGroup) {
    if (TRTCPlatform.isIOS) {
      TRTCMethodChannel().startScreenCaptureByReplaykit(streamType, encParam, appGroup);
    } else {
      TRTCLog(_tag, "startScreenCaptureByReplaykit is not currently supported on this platform");
    }
  }

  @override
  int startSpeedTest(TRTCSpeedTestParams params) {
    TRTCLog(_tag, "startSpeedTest: $params");
    return TRTCCloudNative.instance.startSpeedTest(params);
  }

  @override
  void startSystemAudioLoopback({String? deviceName}) {
    TRTCLog(_tag, "startSystemAudioLoopback: $deviceName");
    if (Platform.isAndroid || Platform.isIOS) {
      debugPrint("trtc-api not support");
      return;
    }
    TRTCCloudNative.instance.startSystemAudioLoopback(deviceName: deviceName);
  }

  @override
  void stopLocalRecording() {
    TRTCLog(_tag, "stopLocalRecording");
    TRTCCloudNative.instance.stopLocalRecording();
  }

  @override
  void stopPublishMediaStream(String taskId) {
    TRTCLog(_tag, "stopPublishMediaStream: $taskId");
    TRTCCloudNative.instance.stopPublishMediaStream(taskId);
  }

  @override
  void stopScreenCapture() {
    TRTCLog(_tag, "stopScreenCapture");
    TRTCCloudNative.instance.stopScreenCapture();
  }

  @override
  void stopSpeedTest() {
    TRTCLog(_tag, "stopSpeedTest");
    TRTCCloudNative.instance.stopSpeedTest();
  }

  @override
  void stopSystemAudioLoopback() {
    TRTCLog(_tag, "stopSystemAudioLoopback");
    if (Platform.isAndroid || Platform.isIOS) {
      debugPrint("trtc-api not support");
      return;
    }
    TRTCCloudNative.instance.stopSystemAudioLoopback();
  }

  @override
  void updatePublishMediaStream(String taskId, TRTCPublishTarget target, TRTCStreamEncoderParam param, TRTCStreamMixingConfig config) {
    TRTCLog(_tag, "updatePublishMediaStream: $taskId $target $param $config");
    TRTCCloudNative.instance.updatePublishMediaStream(taskId, target, param, config);
  }

  @override
  void enableCustomVideoCapture(TRTCVideoStreamType streamType, bool enable) {
    TRTCLog(_tag, "enableCustomVideoCapture: $streamType $enable");
    TRTCCloudNative.instance.enableCustomVideoCapture(streamType, enable);
  }

  @override
  void muteAllRemoteVideoStreams(bool mute) {
    TRTCLog(_tag, "muteAllRemoteVideoStreams: $mute");
    TRTCCloudNative.instance.muteAllRemoteVideoStreams(mute);
  }

  @override
  void setSystemAudioLoopbackVolume(int volume) {
    TRTCLog(_tag, "setSystemAudioLoopbackVolume: $volume");
    if (Platform.isAndroid || Platform.isIOS) {
      debugPrint("trtc-api not support");
      return;
    }
    TRTCCloudNative.instance.setSystemAudioLoopbackVolume(volume);
  }

  @override
  void setVideoMuteImage(String imagePath, int fps) {
    if (Platform.isWindows || Platform.isMacOS) {
      TRTCLog(_tag, "setVideoMuteImage is not currently supported on this platform");
      return;
    }
    TRTCLog(_tag, "setVideoMuteImage: $imagePath $fps");
    TRTCMethodChannel().setVideoMuteImage(imagePath, fps);
  }

  @override
  void showDebugView(int showType) {
    TRTCLog(_tag, "showDebugView: $showType");
    TRTCCloudNative.instance.showDebugView(showType);
  }

  @override
  void snapshotVideo(String userId, TRTCVideoStreamType streamType, TRTCSnapshotSourceType sourceType, {String? path}) {
    if (Platform.isWindows || Platform.isMacOS) {
      TRTCLog(_tag, "snapshotVideo is not currently supported on this platform");
      return;
    }
    TRTCLog(_tag, "snapshotVideo: $userId $streamType $sourceType");
    TRTCMethodChannel().snapshotVideo(userId, streamType, sourceType, path ?? "");
  }

  @override
  void updateLocalView(int viewId) {
    TRTCLog(_tag, "updateLocalView: $viewId");
    if (TRTCPlatform.isMacOS || TRTCPlatform.isWindows) {
      TRTCMethodChannel().updateLocalView(viewId);
      return;
    }
    if (!TRTCCloudVideoView.containsViewId(viewId)) {
      TRTCLog(_tag, "updateLocalView fail, viewId does not exist");
      return;
    }
    TRTCCloudNative.instance.updateLocalView(viewId);
  }

  @override
  void updateRemoteView(String userId, TRTCVideoStreamType streamType, int viewId) {
    TRTCLog(_tag, "updateRemoteView: $userId $streamType $viewId");
    if (TRTCPlatform.isMacOS || TRTCPlatform.isWindows) {
      TRTCMethodChannel().updateRemoteView(userId, streamType, viewId);
      return;
    }
    if (!TRTCCloudVideoView.containsViewId(viewId)) {
      TRTCLog(_tag, "updateRemoteView fail, viewId does not exist");
      return;
    }
    TRTCCloudNative.instance.updateRemoteView(userId, streamType, viewId);
  }

  @override
  void setConsoleEnabled(bool enabled) {
    TRTCLog(_tag, "setConsoleEnabled: $enabled");
    _logParams.consoleEnabled = enabled;
    TRTCCloudNative.instance.setLogParams(_logParams);
  }

  @override
  void setLogCompressEnabled(bool enabled) {
    TRTCLog(_tag, "setLogCompressEnabled: $enabled");
    _logParams.compressEnabled = enabled;
    TRTCCloudNative.instance.setLogParams(_logParams);
  }

  @override
  void setLogDirPath(String path) {
    TRTCLog(_tag, "setLogDirPath: $path");
    _logParams.filePath = path;
    TRTCCloudNative.instance.setLogParams(_logParams);
  }

  @override
  void setLogLevel(TRTCLogLevel level) {
    TRTCLog(_tag, "setLogLevel: $level");
    _logParams.level = level;
    TRTCCloudNative.instance.setLogParams(_logParams);
  }

  @override
  void setBeautyStyle(TRTCBeautyStyle style, int beautyLevel, int whitenessLevel, int ruddinessLevel) {
    TRTCCloudNative.instance.setBeautyStyle(style, beautyLevel, whitenessLevel, ruddinessLevel);
  }
}